package com.mouse.hxsj.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageUtil {
    public static boolean screenImage(int x,int y,int x1,int y1,String path){
        Rectangle rect = new Rectangle(x, y,x1,y1);
        try {
            BufferedImage bufferedImage=captureScreen(rect);
            saveImage(bufferedImage, path);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    public static BufferedImage captureScreen(Rectangle screenRect)
            throws AWTException{
        Robot robot = new Robot();
        return robot.createScreenCapture(screenRect);
    }

    public static void saveImage(BufferedImage srcImage, String saveImagePath) throws IOException {
        File file = new File(saveImagePath);
        file.mkdir();
        String suffix = saveImagePath.substring(saveImagePath.lastIndexOf('.')+1);
        ImageIO.write(srcImage, suffix, file);
    }
}
